package awareness.gui;

import awareness.AwarenessMidlet;
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.media.Manager;
import javax.microedition.media.Player;

public class AwarenessCanvas extends Canvas implements Runnable {

    private LoadingBar bar = null;
    int barPositionX;
    int barPositionY;
    private Image[] images;
    private String[] sounds = {"/5.wav", "/1.wav", "/2.wav", "/3.wav", "/4.wav", "/5.wav"};
    private int imageToShow = 0;
    private String[] text;
    public static boolean showLoadingBar = false;
    private AwarenessMidlet midlet;

    public AwarenessCanvas(AwarenessMidlet midlet) {

        this.midlet = midlet;

        bar = new LoadingBar(160, 3, 5, 20, 0xd0282d);
        barPositionX = 81;
        barPositionY = getHeight() - 10;

        images = new Image[6];
        try {
            images[0] = Image.createImage("/logo3.png");
            for (int i = 1; i < 6; i++) {
                images[i] = Image.createImage("/" + (i) + ".png");
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        imageToShow = 0;

        showLoadingBar = true;

        bar.start();

        new Thread(this).start();
    }

    protected void paint(Graphics g) {

        if (AwarenessMidlet.agreeIndex != -1) {
            imageToShow = AwarenessMidlet.agreeIndex / 21 + 1;
        } else {
            imageToShow = 0;
        }

        g.setColor(0xfbd8aa);

        g.fillRect(0, 0, getWidth(), getHeight());

        if (showLoadingBar) {
            imageToShow = 0;
            g.translate(barPositionX, barPositionY);

            bar.paint(g);

            g.translate(-barPositionX, -barPositionY);

            g.setColor(0xd0282d);
            g.setFont(Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN, Font.SIZE_MEDIUM));
            g.drawString("Searching", barPositionX / 2, barPositionY + 2, Graphics.HCENTER | Graphics.BASELINE);
        }

        g.setColor(0xd0282d);
        g.setFont(Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_LARGE));

        if (imageToShow != 0) {
                if (AwarenessMidlet.index == 0) {
                    if (AwarenessMidlet.language == 1) { // english
                        g.drawString(AwarenessMidlet.english[0] + AwarenessMidlet.agreeIndex + "%", 120, 25, Graphics.HCENTER | Graphics.BASELINE);
                        g.drawString(AwarenessMidlet.english[1], 120, 45, Graphics.HCENTER | Graphics.BASELINE);
                        g.drawString(AwarenessMidlet.english[2], 120, 65, Graphics.HCENTER | Graphics.BASELINE);
                    } else { // eesti
                        g.drawString(AwarenessMidlet.eesti[0], 120, 25, Graphics.HCENTER | Graphics.BASELINE);
                        g.drawString(AwarenessMidlet.eesti[1] + AwarenessMidlet.agreeIndex + "%" + AwarenessMidlet.eesti[2], 120, 45, Graphics.HCENTER | Graphics.BASELINE);
                        g.drawString(AwarenessMidlet.eesti[3], 120, 65, Graphics.HCENTER | Graphics.BASELINE);
                    }
                } else if (AwarenessMidlet.index != 0) {
                    if (AwarenessMidlet.language == 1) { // english
                        g.drawString(AwarenessMidlet.english2[0], 120, 25, Graphics.HCENTER | Graphics.BASELINE);
                        g.drawString(AwarenessMidlet.agreeIndex + "%" + AwarenessMidlet.english2[1], 120, 45, Graphics.HCENTER | Graphics.BASELINE);
                        try {
                            g.drawString(AwarenessMidlet.english2[2] + midlet.inquiry_listener.getRemoteDevice(midlet.index).getFriendlyName(false).substring(3), 120, 65, Graphics.HCENTER | Graphics.BASELINE);
                        } catch (IOException e) {
                        }
                    } else { // eesti
                        g.drawString(AwarenessMidlet.eesti2[0], 120, 25, Graphics.HCENTER | Graphics.BASELINE);
                        g.drawString(AwarenessMidlet.agreeIndex + "%", 120, 45, Graphics.HCENTER | Graphics.BASELINE);
                        try {
                            g.drawString(AwarenessMidlet.eesti2[1] + midlet.inquiry_listener.getRemoteDevice(midlet.index).getFriendlyName(false).substring(3), 120, 65, Graphics.HCENTER | Graphics.BASELINE);
                        } catch (IOException e) {
                        }
                    }
            }
        } else if (!midlet.isNextToYou && !showLoadingBar) {
                if (AwarenessMidlet.language == 1) { // english
                    try {
                        g.drawString(AwarenessMidlet.english3[0] + midlet.inquiry_listener.getRemoteDevice(midlet.index).getFriendlyName(false).substring(3), 120, 25, Graphics.HCENTER | Graphics.BASELINE);
                    } catch (IOException e) {
                    }
                    g.drawString(AwarenessMidlet.english3[1], 120, 45, Graphics.HCENTER | Graphics.BASELINE);
                    g.drawString(AwarenessMidlet.english3[2], 120, 65, Graphics.HCENTER | Graphics.BASELINE);
                } else { // eesti
                    g.drawString(AwarenessMidlet.eesti3[0], 120, 25, Graphics.HCENTER | Graphics.BASELINE);
                    try {
                        g.drawString(AwarenessMidlet.eesti3[1] + midlet.inquiry_listener.getRemoteDevice(midlet.index).getFriendlyName(false).substring(3), 120, 45, Graphics.HCENTER | Graphics.BASELINE);
                    } catch (IOException e) {
                    }
                    g.drawString(AwarenessMidlet.eesti3[2], 120, 65, Graphics.HCENTER | Graphics.BASELINE);
                    g.drawString(AwarenessMidlet.eesti3[3], 120, 85, Graphics.HCENTER | Graphics.BASELINE);
                }
            }


        g.drawImage(images[imageToShow], getWidth() / 2, getHeight() / 2 + 25, Graphics.HCENTER | Graphics.VCENTER);
    }

    public void run() {
        while (true) {
            try {
                repaint();

                synchronized (this) {
                    wait(50L);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void setTextLanguage(String[] t) {
        text = t;
    }

    public void playSound() {
        try {
            InputStream in = getClass().getResourceAsStream(sounds[imageToShow]);
            Player player = Manager.createPlayer(in, "audio/x-wav");
            player.prefetch();
            player.start();
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
